package cn.lotusnut.rabbitmqboot.configuration;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author JiangMing Zhou
 * @Date 2022/12/1 14:14
 * @Version 1.8
 * rabbitmq配置类，配置消息队列，交换机，绑定消息队列，交换机
 */
@Configuration
public class RabbitmqConfiguration {

    /**
     * simple模式
     */
    @Bean
    public Queue simpleQueue(){
        return new Queue("simple_queue");
    }


    /**
     * work模式
     * @return
     */
    @Bean
    public Queue workQueue(){
        return new Queue("work_queue");
    }


    /**
     * 发布订阅模式
     */
    @Bean
    public Queue publishQueueA(){
        return new Queue("publish_a");
    }

    @Bean
    public Queue publishQueueB(){
        return new Queue("publish_b");
    }

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanout_exchange");
    }

    /**
     * 绑定消息队列
     */
    @Bean
    public Binding bindingPublishQueueAToFanoutExchange(Queue publishQueueA,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(publishQueueA).to(fanoutExchange);
    }
    @Bean
    public Binding bindingPublishQueueBToFanoutExchange(Queue publishQueueB,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(publishQueueB).to(fanoutExchange);
    }

    /**
     * 路由模式
     */
    @Bean
    public DirectExchange directExchange(){
        return new DirectExchange("direct_exchange");
    }

    @Bean
    public Queue directQueueRed(){
        return new Queue("direct_red");
    }

    @Bean
    public Queue directQueueGreen(){
        return new Queue("direct_green");
    }

    @Bean
    public Binding bindingRouteQueueRedToDirectExchange(Queue directQueueRed,DirectExchange directExchange){
        return BindingBuilder.bind(directQueueRed).to(directExchange).with("red");
    }

    @Bean
    public Binding bindingRouteQueueGreenToDirectExchange(Queue directQueueGreen,DirectExchange directExchange){
        return BindingBuilder.bind(directQueueGreen).to(directExchange).with("green");
    }

    /**
     * 主题模式
     */
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("topic_exchange");
    }

    @Bean
    public Queue topicQueueCat(){
        return new Queue("topic_cat");
    }

    @Bean
    public Queue topicQueueDog(){
        return new Queue("topic_dog");
    }

    @Bean
    public Binding bindingTopicQueueCatToTopicExchange(Queue topicQueueCat,TopicExchange topicExchange){
        return BindingBuilder.bind(topicQueueCat).to(topicExchange).with("cat.#");
    }

    @Bean
    public Binding bindingTopicQueueDogToTopicExchange(Queue topicQueueDog,TopicExchange topicExchange){
        return BindingBuilder.bind(topicQueueDog).to(topicExchange).with("#.dog");
    }

}
